﻿/*
 * Author: Branimir Topic (uxxx4663)
 * For: UC - ODD - Assignment 2B
 * Purpose: REFACTORING 2C - using design pattern code is reduced and reused.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using ARMS.Models;

namespace ARMS.BaseClasses
{
    public class Factory
    {
        public const string LCUSTOMERS = "Customers";
        public const string LSERVICES = "Services";
        public const string LWORKERS = "Workers";
        public const string CSHOP = "Shop";
        public const string VERROR = "Error";
        
        //Shop will be used in view as a ViewData[CSHOP] to display shop data.
        public static void SetShop(int shopid, Controller controller)
        {
            ShopModel model = new ShopModel();
            controller.ViewData[CSHOP] = model.GetShop(shopid);
        }

        //Bookings are used in Booking controller and they are returned
        public static BookingList GetBookings(int shopid, bool activeOnly)
        {
            BookingModel model = new BookingModel();
            return model.GetBookings(shopid, activeOnly);
        }

        public static void SetServiceList(int shopid, Controller controller)
        {
            InventoryModel model = new InventoryModel();
            System.Data.DataTable services = model.getServices(shopid);

            if (services.Rows.Count < 1)
            {
                throw new Exception("No services found for this shop!");
            }

            string text, value;
            List<SelectListItem> serviceList = new List<SelectListItem>();

            foreach (System.Data.DataRow row in services.Rows)
            {
                value = row["id"].ToString();
                text = String.Format("{0}", row["Description"].ToString());
                serviceList.Add(new SelectListItem { Text = text, Value = value, Selected = false });
            }
            controller.ViewData[LSERVICES] = serviceList;
        }

        public static void SetCustomerList(int shopid, string memberno, string customername, Controller controller)
        {
            // CUSTOMERS ======================================================================
            CustomerModel modelCustomer = new CustomerModel();
            System.Data.DataTable customers = modelCustomer.FindCustomer(shopid, memberno, customername);

            if (customers.Rows.Count < 1)
            {
                controller.ModelState.AddModelError("customerid", "Customer not found");
                throw new Exception("Customer not found!");
            }

            List<SelectListItem> customerList = new List<SelectListItem>();

            bool isselected = true;
            string text, value;
            foreach (System.Data.DataRow r in customers.Rows)
            {
                value = r["id"].ToString();
                text = String.Format("{0} {1}",
                    r["firstname"].ToString(),
                    r["lastname"].ToString());

                customerList.Add(new SelectListItem { Text = text, Value = value, Selected = isselected });
                isselected = false;
            }
            controller.ViewData[LCUSTOMERS] = customerList;
        }

        public static void SetWorkerList(int shopid, Controller controller)
        {
            WorkerModel modelWorker = new WorkerModel();
            System.Data.DataTable workers = modelWorker.getWorkers(shopid);

            if (workers.Rows.Count < 1)
            {
                throw new Exception("No services found for this shop!");
            }

            string text, value = String.Empty;
            List<SelectListItem> workersList = new List<SelectListItem>();

            foreach (System.Data.DataRow row in workers.Rows)
            {
                value = row["id"].ToString();
                text = String.Format("{0} {1}",
                    row["firstname"].ToString(),
                    row["lastname"].ToString()
                );
                workersList.Add(new SelectListItem { Text = text, Value = value, Selected = false });
            }
            controller.ViewData[LWORKERS] = workersList;
        }
    }
}